package com.qsmx.store.app.main.gift;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qsmx.store.app.R;
import com.qsmx.store.app.R2;
import com.qsmx.store.app.fustion.FustionManager;
import com.qsmx.store.app.http.HttpHelper;
import com.qsmx.store.app.http.HttpUrl;
import com.qsmx.store.app.main.gift.adapter.GiftAdapter;
import com.qsmx.store.app.main.gift.adapter.GiftCreateAdapter;
import com.qsmx.store.app.main.goods.adapter.GoodsCreateAdapter;
import com.qsmx.store.app.main.goods.adapter.GoodsListAdapter;
import com.qsmx.store.app.main.goods.entity.GoodsEntity;
import com.qsmx.store.app.util.DisplayUtil;
import com.qsmx.store.core.delegates.AtmosDelegate;
import com.qsmx.store.core.net.callback.IError;
import com.qsmx.store.core.net.callback.IFailure;
import com.qsmx.store.core.net.callback.ISuccess;

import java.lang.reflect.Type;
import java.util.List;
import java.util.WeakHashMap;

import butterknife.BindView;

public class GiftSaleDelegate extends AtmosDelegate {

    GiftAdapter mAdapter;
    GiftCreateAdapter mCreateAdapter;
    private List<GoodsEntity> mGoodsData;

    @BindView(R2.id.rlv_gift)
    RecyclerView rlvGift;
    @BindView(R2.id.rlv_create_order)
    RecyclerView rlvCreateOrder;

    @Override
    public Object setLayout() {
        return R.layout.delegate_gift_sale;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {

        initRecycler();
        initGoodsData();
    }

    private void initRecycler() {

        mAdapter = new GiftAdapter(getContext());
        mCreateAdapter = new GiftCreateAdapter(getContext());
        LinearLayoutManager createManager = new LinearLayoutManager(getContext());
        GridLayoutManager manager = new GridLayoutManager(getContext(), 3);

        rlvGift.setLayoutManager(manager);
        rlvGift.setAdapter(mAdapter);

        rlvCreateOrder.setLayoutManager(createManager);
        rlvCreateOrder.setAdapter(mCreateAdapter);

        DisplayUtil.showPayGiftDisplay(getContext());

    }

    private void initGoodsData() {

        WeakHashMap<String, Object> params = new WeakHashMap<>();
        params.put("deptId", FustionManager.mLoginData.getUser().getUser().getDept().getId());
        params.put("storageType", "2");

        HttpHelper.RestClientGet(params, HttpUrl.GET_GOODS_SALE_INFO,
                new ISuccess() {
                    @Override
                    public void onSuccess(int code, String response) {
                        Type type = new TypeToken<List<GoodsEntity>>() {
                        }.getType();
                        Gson gson = new Gson();
                        mGoodsData = gson.fromJson(response, type);
//                        mAdapter.setData(mGoodsData);
                        mAdapter.notifyDataSetChanged();

                    }
                }, new IError() {
                    @Override
                    public void onError(int code, String msg) {

                    }
                }, new IFailure() {
                    @Override
                    public void onFailure() {

                    }
                });

    }
}
